import { ModelType } from '@/common-typings';
import actions from '@/services/actions';

const exportModel: ModelType = {
  namespace: 'exportExcel',

  state: {
    downloadList: [],
  },

  effects: {
    *getExportManageList({ payload, fn }, { call, put }) {
      const { data } = yield call(actions.getExportManageList, payload);
      if (fn) fn();
      yield put({ key: 'exportExcel/list', type: 'save', payload: data });
    },
    *clearExportManageAll({ payload, fn }, { call, put }) {
      const { data } = yield call(actions.clearExportManageAll, payload);
      if (fn) fn();
      yield put({ key: 'exportExcel/clear', type: 'save', payload: data || [] });
    },
    *clearExportManageById({ payload, fn }, { call, put }) {
      const { data } = yield call(actions.clearExportManageById, payload);
      if (fn) fn();
      yield put({ key: 'exportExcel/clearById', type: 'save', payload: data || [] });
    },
  },

  reducers: {
    save(state: any, action) {
      let result = {};
      switch (action.key) {
        case 'exportExcel/list':
          result = action.payload || [];
          break;
        case 'exportExcel/clear':
          result = state.downloadList;
          break;
        case 'exportExcel/clearById':
          result = state.downloadList;
          break;
        default:
          break;
      }
      return {
        ...state,
        downloadList: result,
      };
    },
  },
};

export default exportModel;
